package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.service.omc.dao.OdsBillMapper;
import com.yuke.cloud.service.omc.dto.OdsBillInfoDto;
import com.yuke.cloud.service.omc.entity.OdsBill;
import com.yuke.cloud.service.omc.service.OdsBillService;
import com.yuke.cloud.common.core.support.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * Created by zn on 2019/12/10.
 */
@Service
@Transactional
public class OdsBillServiceImpl extends BaseService<OdsBill> implements OdsBillService {
    @Resource
    private OdsBillMapper odsBillMapper;

    @Override
    public OdsBill selectLastOneByUserId(Long userId){
        return odsBillMapper.selectLastOneByUserId(userId);
    }

    @Override
    public OdsBillInfoDto getTotalBill(OdsBillInfoDto odsBillInfoDto) {
        OdsBillInfoDto totalBill=null;

        if (odsBillInfoDto!=null&&odsBillInfoDto.getCreateTime()!=null){
            String createTime = odsBillInfoDto.getCreateTime();
            //前端传过来格式为"2019-12" 随意拼接的字符"-01"是为了 sql中 YEAR函数起作用
            odsBillInfoDto.setCreateTime(createTime+"-01");
            String[] split = createTime.split("-");

            odsBillInfoDto.setNian(split[0]);
            System.out.println(split[0]);

            odsBillInfoDto.setYue(split[1]);
            System.out.println(split[1]);

            totalBill = odsBillMapper.getTotalBill(odsBillInfoDto);
            totalBill.setCreateTime(odsBillInfoDto.getCreateTime());
            totalBill.setUserId(odsBillInfoDto.getUserId());
            if (totalBill!=null){
                if (totalBill.getRecharge()==null){
                    totalBill.setRecharge(0);
                }
                if (totalBill.getExpenditure()==null){
                    totalBill.setExpenditure(0);
                }
            }

        }
        return totalBill;
    }

}
